var  Utils = {
    variable :  {
        url: 'https://hanjiang.wei3ma.com/api/',
        menu: [
            {title:'首页', icon:'icon ion-home'},
            {title:'商品', icon:'icon ion-android-apps'},
            {title:'发现', icon:'icon ion-cube'},
            {title:'购物车', icon:'icon ion-ios-cart'},
            {title:'我的', icon:'icon ion-person'}
        ],
        color: "#ee6c21" //主题色
    },
    http(u, d = '', s = 'json', token = '') {
        var m = d ? 'POST' : 'GET';
        token === '' ? token = this.getCookie("token") : '';
        return new Promise(function(resolve, reject) {
            $.ajax({
                method: m,
                dataType: s,
                async: true,//使用同步的方式,true为异步方式
                headers: {
	              'Authori-zation' : 'Bearer ' + token
	            },
                url: u,
                data: d,
                success: function(res, xhr) {
                    resolve(res);
                }
            });
        });
    },
	//取回cookie
	getCookie (cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		var val;
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1);
			if (c.indexOf(name) != -1) val = c.substring(name.length, c.length);
		}
		(!val || typeof val == 'undefined' || val == 'undefined' || val == 'null') ? val = '' : '';
		return val;
	},
    link(u) {
        for (var i in u){
            var d,
            	l = u[i],
            	r = Math.random();
            	
            if ((l + '?').indexOf('.js')!=-1){
                d = document.createElement('script');
                d.type = "text/javascript";
                d.src = l + '?v=' + r;
            } else if ((l + '?').indexOf('.css')!=-1){
                d = document.createElement('link');
                d.rel = "stylesheet";
                d.href = l + '?v=' + r;
            }
            document.getElementsByTagName('head')[0].appendChild(d);
        }
    },
    Url:{
        Get : function(item){
            var svalue = location.search.match(new RegExp("[\?\#\&]" + item + "=([^\&]*)(\&?)","i"));
            return svalue ? svalue[1] : svalue;
        },
		GetAll: function () {
		    var url = decodeURIComponent(location.search); //获取url中"?"符后的字串
		    console.log(url);
		    var theRequest = {};
		    if (url.indexOf("?") != -1) {
		        let str = url.substr(1);
		        strs = str.split("&");
		        for (var i = 0; i < strs.length; i++) {
		            let tempArr = strs[i].split("=");
		            // 类型转换
		            if (tempArr[1] == 'true') {
		                tempArr[1] = true;
		            }
		            if (tempArr[1] == 'false') {
		                tempArr[1] = false;
		            }
		            //if (/^[\d|.]+$/.test(tempArr[1])) {
		            //    tempArr[1] = Number(tempArr[1]);
		            //}
		            // 写入对象
		            if (tempArr[0].search(/\[.*]/) == -1) {
		                theRequest[tempArr[0]] = tempArr[1];
		            } else {
		                // 数组
		                var key = tempArr[0].replace(/\[.*]/, '');
		                if (!theRequest[key]) {
		                    theRequest[key] = [tempArr[1]];
		                } else {
		                    theRequest[key].push(tempArr[1]);
		                }
		            }
		        }
		    }
		    
		    return theRequest;
		},
        Replace(url, arr){
        	var u  = '';
			for(var key in arr){
			    if (arr[key] || arr[key]===0 || arr[key]==='0') {u += '&' + key +'='+ arr[key];}
			}
			u.length > 0 ? url = url + "?" + u : '';
			url = url.replace(/\?&/,'?');
			//history.pushState({}, "页面标题", url);
			history.replaceState({}, '', url);
        }
    },
    href(u) {
		location.replace(u);	
	}
};

export {Utils};
